package createArff;

import Types.DrugDDIFile;
import Types.Sentence;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CreateArffFile {

    public void writeAttributes(ListOfWords listOfWords) {
        List<String> lstWords = listOfWords.getLstWords();
        //Very slow with this code
//        for (int i = 0; i < lstWords.size(); i++) {
//            arfFormat += "@attribute " + lstWords.get(i) + " numeric\n";
//        }
//        List<String> lstBiWords = listOfWords.getLstWords();
//        for (int i = 0; i < lstBiWords.size(); i++) {
//            arfFormat += "@attribute " + lstBiWords.get(i) + " numeric\n";
//        }

        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bin/arff/out.arff"), "UTF8"));
            bw.write("@relation 'ddiExtractorFeatures'");
            bw.write("\n\n");
            for (int i = 0; i < lstWords.size(); i++) {
                bw.write("@attribute " + lstWords.get(i) + " numeric\n");
            }
            List<String> lstBiWords = listOfWords.getLstBiWords();
            for (int i = 0; i < lstBiWords.size(); i++) {
                bw.write("@attribute " + lstBiWords.get(i) + " numeric\n");
            }
            //bw.write("@attribute numberOfWords numeric\n");
            //bw.write("@attribute numberOfWordsBetweenEn numeric\n");
            
            bw.write("@attribute NominalFeature {effect,advise,mechanism,int,no}\n");
            //bw.write("@attribute NominalFeature {effect,advise,mechanism,int}\n");
            //bw.write("@attribute NominalFeature {yes,no}\n");

            bw.write("\n@data\n");
            bw.close();
        } catch (Exception e) {
            System.out.println("exception: " + e);
        }
    }

    public void writeData(List<DrugDDIFile> files, ListOfWords listOfWords) {
        List<String> lstWords = listOfWords.getLstWords();
        List<String> lstBiWords = listOfWords.getLstBiWords();
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bin/arff/out.arff", true), "UTF8"));
            StringBuilder row = new StringBuilder("");
            for (int i = 0; i < files.size(); i++) {
                List<Sentence> lstSentence = files.get(i).getLstSentence();
                for (int j = 0; j < lstSentence.size(); j++) {
                    row = new StringBuilder("");
                    Sentence sentence = lstSentence.get(j);
                    String simplifiedText = sentence.getSimplifiedText();
                    String[] terms = simplifiedText.split(" ");
                    HashMap<Integer, Integer> hmIndexes = new HashMap<Integer, Integer>();
                    for (String s : terms) {
                        if (lstWords.contains(s)) {
                            if (hmIndexes.containsKey(lstWords.indexOf(s))) {
                                hmIndexes.put(lstWords.indexOf(s), hmIndexes.get(lstWords.indexOf(s)) + 1);
                            } else {
                                hmIndexes.put(lstWords.indexOf(s), 1);
                            }
                        }
                    }
                    for (int k = 0; k < lstWords.size(); k++) {
                        if (hmIndexes.containsKey(k)) {
                            //bw.write(String.valueOf(hmIndexes.get(k)));
                            row.append(String.valueOf(hmIndexes.get(k)));
                        } else {
                            //bw.write("0");
                            row.append("0");
                        }
                        //bw.write(",");
                        row.append(",");
                    }

                    for (int m = 0; m < terms.length - 1; m++) {
                        if (terms[m].length() > 0) {
                            String bi = terms[m] + "_" + terms[m + 1];
                            if (lstBiWords.contains(bi)) {
                                if (hmIndexes.containsKey(lstBiWords.indexOf(bi))) {
                                    hmIndexes.put(lstBiWords.indexOf(bi), hmIndexes.get(lstBiWords.indexOf(bi)) + 1);
                                } else {
                                    hmIndexes.put(lstBiWords.indexOf(bi), 1);
                                }
                            }
                        }
                    }
                    for (int k = 0; k < lstBiWords.size(); k++) {
                        if (hmIndexes.containsKey(k)) {
                            //bw.write(String.valueOf(hmIndexes.get(k)));
                            row.append(String.valueOf(hmIndexes.get(k)));
                        } else {
                            //bw.write("0");
                            row.append("0");
                        }
                        //bw.write(",");
                        row.append(",");
                    }


//                    row = row + String.valueOf(simplifiedText.length()));
//                    row = row + ",";

                    ////calculating number of words between drugs

                    // row = row + String.valueOf(returnNumberOfWordsBetweenDrugs(simplifiedText));
                    //row = row + ",";
                    if (sentence.getDdiList().size() > 0) {
                        //bw.write(sentence.getDdiList().get(0).getType_text());
                        //bw.write("yes");
                        row.append(sentence.getDdiList().get(0).getType_text());
                        //row.append("yes");
                    } else {
                        //bw.write("no");
                        row.append("no");
                    }
                    bw.write(row.toString());
                    bw.write("\n");

                }
            }
            bw.close();
        } catch (Exception e) {
            System.out.println("exception: " + e);
        }
    }

    public int returnNumberOfWordsBetweenDrugs(String text) {
        List<String> lstSomeWords = new ArrayList<String>();
        int firstDrug = text.indexOf("replaceddrug");
        if (firstDrug > -1) {
            int secondDrug = text.indexOf("replaceddrug", firstDrug + 12);
            if (secondDrug > -1) {
                String betPart = text.substring(firstDrug + 12, secondDrug).trim();
                String[] temAr = betPart.split(" ");
                for (int p = 0; p < temAr.length; p++) {
                    if (temAr[p].length() > 1) {
                        lstSomeWords.add(temAr[p]);
                    }
                }
            }
        }
        return lstSomeWords.size();
    }
}
